package com.itheima.producer_consumer;

public class Consumer extends Thread {
    Desk desk;
    public Consumer(Desk desk) {
       this.desk=desk;
    }

    @Override
    public void run() {
        //不停地吃
        while (true) {
            //加锁
            synchronized (desk.getLock()) {
                //吃了十个停下
                if (desk.getCount() == 0) {
                    break;
                }
                //判断标记
                if (!desk.isFlag()) {
                    //等待--锁对象调用方法
                    try {
                       desk.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    //吃
                    System.out.println("吃一个汉堡包");
                    //改标记
                    boolean flag = desk.isFlag();
                    flag = false;
                    //吃一个少一个
                    int count = desk.getCount();
                    count--;
                    //唤醒等待线程
                    desk.getLock().notifyAll();
                }
            }
        }
    }
}
